package com.liuhouse.strame;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.*;
import java.util.stream.Stream;

public class LambdaDemo01 {

    public static void main(String[] args) {
        //调用方法 里面执行的代码  每次都会去调用这个方法  作用到每个元素上
//        printNum2(value -> value % 2 == 0, value -> value > 4);
//        testOr();

//        testNegate();

//        test0111();

//        test0222();


//        test27();

//        test28();

        test29();


    }

    private static void test29() {
        Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        Integer integer = integerStream.parallel().peek(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer num) {
                        System.out.println(num + Thread.currentThread().getName());
                    }
                }).filter(num -> num > 5)
                .reduce((result, num) -> result + num).orElseGet(() -> 0);
        System.out.println(integer);

    }

    private static void test28() {
        Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        Integer integer = stream.parallel().filter(num -> num > 5)
                .reduce((result, elem) -> result + elem)
                .get();
        System.out.println(integer);
    }

    private static void test27() {
        List<Author> authors = getAuthors();
        authors.stream()
                .map(author -> author.getAge())
                .map(age -> age + 10)
                .filter(age -> age > 18)
                .map(age -> age + 2)
                .forEach(System.out::println);
        System.out.println("------------------");
        authors.stream()
                .mapToInt(author -> author.getAge())
                .map(age -> age + 10)
                .filter(age -> age > 18)
                .map(age -> age + 2)
                .forEach(System.out::println);

    }

    private static void test0222() {

        List<Author> authors = getAuthors();
        authors.stream()
                .map(author -> author.getName())
                .map(name -> new StringBuilder(name))
                .map(sb -> sb.append("三更").toString())
                .forEach(System.out::println);
    }

    private static void test0111() {
        List<Author> authors = getAuthors();
        Stream<Author> authorStream = authors.stream();
        StringBuilder sb = new StringBuilder();
        authorStream.map(author -> author.getName())
                .forEach(sb::append);
        System.out.println(sb);
    }

    private static void testNegate() {
        //打印作家中年龄不大于17的作家
        List<Author> authors = getAuthors();
        Stream<Author> stream = authors.stream();
        stream.filter(new Predicate<Author>() {
            @Override
            public boolean test(Author author) {
                return author.getAge() > 17;
            }
        }.negate()).forEach(author -> System.out.println(author.getAge()));
    }

    //测试常用的or
    private static void testOr() {
        //打印作家中年龄大于17或者姓名的长度小于2的作家
        List<Author> authors = getAuthors();
        Stream<Author> stream = authors.stream();
        stream.filter(((Predicate<Author>) author -> author.getAge() > 17)
                .or(author -> author.getName().length() > 2)).forEach(author -> System.out.println(author.getName()));
    }

    public static void printNum2(IntPredicate predicate1 , IntPredicate predicate2){
        int[] arr = {1,2,3,4,5,6,7,8,9,10};
        for(int i : arr){
            //将两个整形计算接口添加到一起 执行 test方法  就证明是要满足两个条件才能够返回
            if(predicate1.and(predicate2).test(i)){
                System.out.println(i);
            }
        }
    }





    private static List<Author> getAuthors(){
        //数据初始化
        Author author1 = new Author(1L, "吴承恩111", 18, "字汝忠，号射阳居士、射阳山人", null);
        Author author2 = new Author(2L, "施耐庵222", 18, "名霑，字梦阮，号雪芹，又号芹圃、芹溪。祖籍辽阳。清朝小说家、诗人、画家。曹寅之孙", null);
        Author author3 = new Author(1L, "吴承恩111", 20, "字汝忠，号射阳居士、射阳山人", null);
        Author author4 = new Author(4L, "罗", 25, "名本，字贯中， [18]号湖海散人， [16-17]太原人。元末明初小说家", null);

        //书籍列表
        ArrayList<Book> books1 = new ArrayList<>();
        ArrayList<Book> books2 = new ArrayList<>();
        ArrayList<Book> books3 = new ArrayList<>();

        //给书籍添加数据
        books1.add(new Book(1L , "刀的两侧是光明与黑暗11","哲学,爱情11" ,85 ,"一刀就会两段啊11"));
        books1.add(new Book(2L , "刀的两侧是光明与黑暗22","个人成长,爱情22" ,90 ,"一刀就会两段啊222"));

        books2.add(new Book(3L , "刀的两侧是光明与黑暗33","个人成长,爱情33" ,91 ,"一刀就会两段啊333"));
        books2.add(new Book(4L , "刀的两侧是光明与黑暗44","个人成长,爱情44" ,60 ,"一刀就会两段啊444"));
        books2.add(new Book(5L , "刀的两侧是光明与黑暗55","个人成长,爱情55" ,50 ,"一刀就会两段啊555"));

        books3.add(new Book(6L , "刀的两侧是光明与黑暗66","个人成长,爱情66" ,50 ,"一刀就会两段啊666"));
        books3.add(new Book(6L , "刀的两侧是光明与黑暗77","个人成长,爱情77" ,88 ,"一刀就会两段啊777"));
        books3.add(new Book(8L , "刀的两侧是光明与黑暗88","个人成长,爱情88" ,30 ,"一刀就会两段啊888"));

        //给作者分配图书
        author1.setBooks(books1);
        author2.setBooks(books2);
        author3.setBooks(books1);
        author4.setBooks(books2);

        //将作者进行合并 ， 合并成一个数组
        List<Author> authorList = new ArrayList<>(Arrays.asList(author1 , author2 , author3 , author4));
        return authorList;
    }

}
